package orm.utils;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.*;
import java.util.List;
import java.util.Map;

@Component
public class PictureUtils implements ImageObserver {

    @Override
    public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
        return false;
    }

    private static void createImage(String fileLocation, BufferedImage image) {
        try {
            FileOutputStream fos = new FileOutputStream(fileLocation);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
            encoder.encode(image);
            bos.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public BufferedImage drawPic(Map zMap, Map xMap, Map winMap, List<Map> zPkp, List<Map> xPkp) throws Exception {

        int imageWidth = 855;// 图片的宽度

        int imageHeight = 454;// 图片的高度

        //定义画布
        BufferedImage image = new BufferedImage(imageWidth, imageHeight,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        String backgroundUrl = this.getClass().getResource("/pkp/background.jpg").getPath();
        BufferedImage backgroundImg = ImageUtil.getBufferedImage(backgroundUrl);
        graphics.drawImage(backgroundImg, 0, 0, backgroundImg.getWidth(this), backgroundImg.getHeight(this), this);//设定位置


        //左侧闲 框
        graphics.setPaint(new Color(6, 44, 229));
        graphics.fillRect(70, 122, 248, 77);


        //右侧庄 框
        graphics.setPaint(new Color(241, 76, 70));
        graphics.fillRect(533, 122, 248, 77);


        Font font = new Font("黑体", Font.BOLD, 72);
        graphics.setFont(font);

        if (winMap.get("赢家").toString().equals("闲家"))
            graphics.setColor(new Color(6, 44, 229));
        graphics.drawString(winMap.get("赢家").toString(), 352, 190);


        //闲
        Font winFont = new Font("黑体", Font.BOLD, 60);
        graphics.setFont(winFont);

        graphics.setColor(Color.WHITE);
        graphics.drawString("闲", 90, 180);
        graphics.drawString(xMap.get("点数").toString(), 200, 180);
        //闲 点数

        //庄
        graphics.setColor(Color.WHITE);
        graphics.drawString("庄", 700, 180);
        graphics.drawString(zMap.get("点数").toString(), 560, 180);

        int minusX = 83;
        int zPkpX = 398;
        int xPkpX = 375;

        for (Map xPkpMap : xPkp) {
            BufferedImage bufferedImage = ImageUtil.getBufferedImage(xPkpMap.get("path").toString());
            graphics.drawImage(bufferedImage, zPkpX -= minusX, 265, bufferedImage.getWidth(this), bufferedImage.getHeight(this), this);//设定位置
        }

        for (Map zPkpMap : zPkp) {
            BufferedImage bufferedImage = ImageUtil.getBufferedImage(zPkpMap.get("path").toString());
            graphics.drawImage(bufferedImage, xPkpX += minusX, 265, bufferedImage.getWidth(this), bufferedImage.getHeight(this), this);//设定位置
        }
//
//        String a1 = this.getClass().getResource("/pkp/a1.png").getPath();
//        String a2 = this.getClass().getResource("/pkp/a2.png").getPath();
//        String a3 = this.getClass().getResource("/pkp/a3.png").getPath();
//        String a4 = this.getClass().getResource("/pkp/a4.png").getPath();
//        String a5 = this.getClass().getResource("/pkp/a11.png").getPath();
//        String a6 = this.getClass().getResource("/pkp/a12.png").getPath();
//        String a7 = this.getClass().getResource("/pkp/a13.png").getPath();
//        String a8 = this.getClass().getResource("/pkp/a10.png").getPath();
//
//        BufferedImage bufferedImage = ImageUtil.getBufferedImage(a1);
//        BufferedImage bufferedImage2 = ImageUtil.getBufferedImage(a2);
//        BufferedImage bufferedImage3 = ImageUtil.getBufferedImage(a3);
//        BufferedImage bufferedImage4 = ImageUtil.getBufferedImage(a4);
//        BufferedImage bufferedImage5 = ImageUtil.getBufferedImage(a5);
//        BufferedImage bufferedImage6 = ImageUtil.getBufferedImage(a6);
//        BufferedImage bufferedImage7 = ImageUtil.getBufferedImage(a7);
//        BufferedImage bufferedImage8 = ImageUtil.getBufferedImage(a8);
//
//
//        graphics.drawImage(bufferedImage, 315, 265, bufferedImage.getWidth(this),bufferedImage.getHeight(this), this);//设定位置
//        graphics.drawImage(bufferedImage2, 315 - minusX, 265, bufferedImage.getWidth(this),bufferedImage.getHeight(this), this);//设定位置
//        graphics.drawImage(bufferedImage3, 315 - minusX * 2, 265, bufferedImage.getWidth(this),bufferedImage.getHeight(this), this);//设定位置
//        graphics.drawImage(bufferedImage4, 315 - minusX * 3, 265, bufferedImage.getWidth(this),bufferedImage.getHeight(this), this);//设定位置
//
//        graphics.drawImage(bufferedImage5, 458, 265, bufferedImage.getWidth(this),bufferedImage.getHeight(this), this);//设定位置
//        graphics.drawImage(bufferedImage6, 458 + minusX, 265, bufferedImage.getWidth(this),bufferedImage.getHeight(this), this);//设定位置
//        graphics.drawImage(bufferedImage7, 458 + minusX * 2, 265, bufferedImage.getWidth(this),bufferedImage.getHeight(this), this);//设定位置
//        graphics.drawImage(bufferedImage8, 458 + minusX * 3, 265, bufferedImage.getWidth(this),bufferedImage.getHeight(this), this);//设定位置

//        createImage("test1.jpg", image);
        return toBufferedImage(image.getScaledInstance(600, 300, Image.SCALE_AREA_AVERAGING));
    }

    public static BufferedImage toBufferedImage(Image image) {
        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }

        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();

        // Determine if the image has transparent pixels; for this method's
        // implementation, see e661 Determining If an Image Has Transparent Pixels
        //boolean hasAlpha = hasAlpha(image);

        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;
           /* if (hasAlpha) {
             transparency = Transparency.BITMASK;
             }*/

            // Create the buffered image
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(
                    image.getWidth(null), image.getHeight(null), transparency);
        } catch (HeadlessException e) {
            // The system does not have a screen
        }

        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            //int type = BufferedImage.TYPE_3BYTE_BGR;//by wang
            /*if (hasAlpha) {
             type = BufferedImage.TYPE_INT_ARGB;
             }*/
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }

        // Copy image to buffered image
        Graphics g = bimage.createGraphics();

        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bimage;
    }
}